const express = require('express');
const path = require('path');
const cors = require('cors');
const jwt = require('express-jwt');
const app = express();
app.listen(4000, () => {
    console.log('server is running');
});
// -----------  配置中间件  ------------
app.use(cors());
app.use(express.urlencoded({ extended: false }));
// 解析token字符串
// 把用户保存的数据 放到req.user 的变量上
app.use(jwt({ secret: 'bigevent' }).unless({ path: /^\/api/ }));
// ----------- 加载路由模块 并配置成中间件 ------------
app.use('/my/article', require(path.join(__dirname, 'routers', 'article')));
app.use('/my/article', require(path.join(__dirname, 'routers', 'category')));
app.use('/api', require(path.join(__dirname, 'routers', 'login')));
app.use('/my', require(path.join(__dirname, 'routers', 'user')));
//错误处理中间件
app.use(function(err, req, res, next) {
    if (err.name === 'UnauthorrizeError') {
        res.status(401).send({
            status: 1,
            message: '身份认证失败！',
        });
    }
});